//
//  CarStateSearchVC.m
//  New_Runway
//
//  Created by mack on 2018/2/7.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarStateSearchVC.h"
#import "CarSetListCell.h"
#import "CarConfigCell.h"

@interface CarStateSearchVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, assign)  BOOL ifLastSelected;
@property (nonatomic, strong)NSIndexPath *lastSelected;//上一次选中的索引

@end

@implementation CarStateSearchVC
-(NSMutableDictionary *)StateDic{
    if (!_StateDic) {
        _StateDic = [[NSMutableDictionary alloc] init];
    }
    return _StateDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"状态";
    [self.navigationBarView.rightItem setTitle:@"确定" forState:UIControlStateNormal];
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
    
    
}
//-(void)setStateDicValue{
//    [self.StateDic setObject:@"" forKey:@"carBargainPriceState"];
//    [self.StateDic setObject:@"" forKey:@"carOrderState"];
//    [self.StateDic setObject:@"" forKey:@"carSaleState"];
//    [self.StateDic setObject:@"" forKey:@"carDetectState"];
//    [self.StateDic setObject:@"" forKey:@"carAuthState"];
//    [self.StateDic setObject:@"" forKey:@"carPledgeState"];
//
//}
-(void)initTableview{
        kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[CarSetListCell class] forCellReuseIdentifier:@"CarSetListCell"];
    [self.tableview registerClass:[CarConfigCell class] forCellReuseIdentifier:@"CarConfigCell"];

    
    [self.view addSubview:self.tableview];
    
    self.tableview.frame = CGRectMake(0, 0, Screen_width, 0);
        [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.view).mas_offset(self.navH);
            make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
            make.left.right.equalTo(weakSelf.view);
        }];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([self.type isEqualToString:kCarInLibary]) {
        return 4;
    }
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        if ([self.type isEqualToString:kCarOrder]) {
            return 1;
        }else if ([self.type isEqualToString:kCarNoLibary]){
            return 4;
        }
        return 3;
    }else if (section == 1){
        return 5;
    }else if (section == 3){
        return 2;
    }
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSArray *array = @[@"仅看特卖",@"仅看已预定",@"已认证",@"仅看已驳回"];
        CarSetListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarSetListCell"];
        cell.centerTF.hidden = YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.centerLabel.hidden = YES;
        cell.rightImage.hidden = YES;
        cell.switchButton.hidden = NO;
        cell.label.text = array[indexPath.row];
        if (indexPath.row == 0) {
            [cell.switchButton setOn:self.ifSelectedOne];
        }else if (indexPath.row == 1){
            [cell.switchButton setOn:self.ifSelectedTwo];
        }else if (indexPath.row == 2){
            [cell.switchButton setOn:self.ifSelectedThree];
        }else if (indexPath.row == 3){
             [cell.switchButton setOn:self.ifSelectedFour];
        }
        return cell;
    }else if (indexPath.section == 1){
   
        CarConfigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarConfigCell"];
        cell.leftImage.image = [UIImage imageNamed:@"circle"];
        NSArray *array = @[@"未初检",@"初检不通过",@"初检通过",@"申请复检中",@"复检完成"];
        if ([self.StateDic[@"carDetectState"] isEqualToString:@"500"]) {
            self.lastSelected = [NSIndexPath indexPathForRow:0 inSection:1];
        }else if ([self.StateDic[@"carDetectState"] isEqualToString:@"510"]){
            self.lastSelected = [NSIndexPath indexPathForRow:1 inSection:1];
        }else if ([self.StateDic[@"carDetectState"] isEqualToString:@"520"]){
            self.lastSelected = [NSIndexPath indexPathForRow:2 inSection:1];
        }else if ([self.StateDic[@"carDetectState"] isEqualToString:@"530"]){
            self.lastSelected = [NSIndexPath indexPathForRow:3 inSection:1];
        }else if ([self.StateDic[@"carDetectState"] isEqualToString:@"540"]){
            self.lastSelected = [NSIndexPath indexPathForRow:4 inSection:1];
        }
        cell.label.text = array[indexPath.row];
        cell.isSelected = NO;
            if (self.lastSelected == indexPath) {
              
                cell.leftImage.image = [UIImage imageNamed:@"hook"];
                cell.isSelected = YES;
            }else{
                cell.leftImage.image = [UIImage imageNamed:@"circle"];
                cell.isSelected = NO;
            }
    
        return cell;
    }else if (indexPath.section == 3){
        CarConfigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarConfigCell"];
        cell.leftImage.image = [UIImage imageNamed:@"circle"];
        NSArray *array = @[@"未抵押",@"已抵押"];
        cell.label.text = array[indexPath.row];
        cell.isSelected = NO;
        if ([self.StateDic[@"carPledgeState"] isEqualToString:@"310"]) {
            self.lastSelected = [NSIndexPath indexPathForRow:1 inSection:3];
        }else if ([self.StateDic[@"carPledgeState"] isEqualToString:@"300"]){
            self.lastSelected = [NSIndexPath indexPathForRow:0 inSection:3];
        }
        if (self.lastSelected == indexPath) {
            
            cell.leftImage.image = [UIImage imageNamed:@"hook"];
            cell.isSelected = YES;
        }else{
            cell.leftImage.image = [UIImage imageNamed:@"circle"];
            cell.isSelected = NO;
        }
        return cell;
    }
    CarConfigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarConfigCell"];
    cell.leftImage.image = [UIImage imageNamed:@"circle"];
    NSArray *array = @[@"已上架",@"未上架",@"已下架"];
    cell.label.text = array[indexPath.row];
    cell.isSelected = NO;
    if ([self.StateDic[@"carSaleState"] isEqualToString:@"20"]) {
        self.lastSelected = [NSIndexPath indexPathForRow:0 inSection:2];
    }else if ([self.StateDic[@"carSaleState"] isEqualToString:@"0"]){
        self.lastSelected = [NSIndexPath indexPathForRow:1 inSection:2];
    }else if ([self.StateDic[@"carSaleState"] isEqualToString:@"40"]){
        self.lastSelected = [NSIndexPath indexPathForRow:2 inSection:2];
    }
    if (self.lastSelected == indexPath) {
        
        cell.leftImage.image = [UIImage imageNamed:@"hook"];
        cell.isSelected = YES;
    }else{
        cell.leftImage.image = [UIImage imageNamed:@"circle"];
        cell.isSelected = NO;
    }
    return cell;
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        if (indexPath.row == 0) {
            if (self.ifSelectedOne == YES) {
                self.ifSelectedOne = NO;
                [self.StateDic setObject:@"" forKey:@"carBargainPriceState"];
            }else{
                self.ifSelectedOne = YES;
                [self.StateDic setObject:@"1" forKey:@"carBargainPriceState"];

            }
        }else if (indexPath.row == 1){
            if (self.ifSelectedTwo == YES) {
                self.ifSelectedTwo = NO;
                [self.StateDic setObject:@"" forKey:@"carOrderState"];

            }else{
                self.ifSelectedTwo = YES;
                [self.StateDic setObject:@"1" forKey:@"carOrderState"];
            }
            
        }else if (indexPath.row == 2){
            if (self.ifSelectedThree == YES) {
                self.ifSelectedThree = NO;
                [self.StateDic setObject:@"" forKey:@"carAuthState"];
                
            }else{
                self.ifSelectedThree = YES;
                [self.StateDic setObject:@"10" forKey:@"carAuthState"];
            }
        }else if (indexPath.row == 3){
            if (self.ifSelectedFour == YES) {
                self.ifSelectedFour = NO;
                [self.StateDic setObject:@"0" forKey:@"carStockState"];
                
            }else{
                self.ifSelectedFour = YES;
                [self.StateDic setObject:@"1" forKey:@"carStockState"];
            }
        }
    }else if (indexPath.section == 1){
      
        CarConfigCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
        if (cell.isSelected == YES) {
            self.lastSelected = [NSIndexPath indexPathForRow:5 inSection:0];
            [self.StateDic setObject:@"" forKey:@"carDetectState"];

        }else{
            self.lastSelected = indexPath;
            if (indexPath.row == 2) {
                [self.StateDic setObject:@"520" forKey:@"carDetectState"];
            }else if (indexPath.row == 1){
                [self.StateDic setObject:@"510" forKey:@"carDetectState"];
            }else if (indexPath.row == 0){
                [self.StateDic setObject:@"500" forKey:@"carDetectState"];
            }else if (indexPath.row == 3){
                [self.StateDic setObject:@"530" forKey:@"carDetectState"];
            }else if (indexPath.row == 4){
                [self.StateDic setObject:@"540" forKey:@"carDetectState"];
            }
        }
        
    }else if (indexPath.section == 2){
        CarConfigCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
        if (cell.isSelected == YES) {
            self.lastSelected = [NSIndexPath indexPathForRow:5 inSection:0];
            [self.StateDic setObject:@"" forKey:@"carSaleState"];

        }else{
            self.lastSelected = indexPath;
            if (indexPath.row == 0) {
                [self.StateDic setObject:@"20" forKey:@"carSaleState"];
                
            }else if (indexPath.row == 1){
                [self.StateDic setObject:@"0" forKey:@"carSaleState"];
                
            }else if (indexPath.row == 2){
                [self.StateDic setObject:@"40" forKey:@"carSaleState"];
                
            }
            
        }
        
    }else if (indexPath.section == 3){
        CarConfigCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
        if (cell.isSelected == YES) {
            self.lastSelected = [NSIndexPath indexPathForRow:5 inSection:0];
            [self.StateDic setObject:@"" forKey:@"carPledgeState"];

        }else{
            self.lastSelected = indexPath;
            if (indexPath.row == 0) {
                [self.StateDic setObject:@"300" forKey:@"carPledgeState"];
                
            }else if (indexPath.row == 1){
                [self.StateDic setObject:@"310" forKey:@"carPledgeState"];
                
            }
        }
        
    }
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    UILabel *titleL = [[UILabel alloc] init];
    titleL.font = kFontSize(14);
    titleL.textColor = MAIN_FONT_COLOR();
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).mas_offset(16);
        make.centerY.equalTo(view);
    }];
    if (section == 1) {
        titleL.text = @"检测状态";
    }else if (section == 2){
        titleL.text = @"上架状态";
    }else if (section == 3){
        titleL.text = @"抵押状态";

    }
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.0001;
    }
    return 25;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)rightItemClick{
    [self.navigationController popViewControllerAnimated:YES];
    self.stateBlock(_StateDic);
}

@end
